package cc.vv.btpartyjob.partymanager.activity

import android.content.Intent
import android.support.v4.widget.SwipeRefreshLayout
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import cc.vv.baselibrary.bean.base.BaseResponseObj
import cc.vv.baselibrary.global.UserInfoSharePreKey
import cc.vv.baselibrary.util.LKPrefUtil
import cc.vv.baselibrary.view.BTLoadMoreView
import cc.vv.baselibrary.view.BaseTopBarView
import cc.vv.btpartyjob.jnimodule.enums.H5UrlEnum
import cc.vv.btpartyjob.jnimodule.globle.BroadCastJniKey
import cc.vv.btpartyjob.jnimodule.server.JniStartActivity
import cc.vv.btpartyjob.jnimodule.server.OperationServer
import cc.vv.btpartyjob.partymanager.R
import cc.vv.btpartyjob.partymanager.adapter.PartyPromisePingJiaListAdapter
import cc.vv.btpartyjob.partymanager.delegate.ManagerBaseBinder
import cc.vv.btpartyjob.partymanager.delegate.PartyPromisePingjiaListActivityDelegate
import cc.vv.btpartyjob.partymanager.moudel.bean.PromisePingJiaObj
import cc.vv.btpartyjob.partymanager.moudel.resp.PromiseDetailResponseObj
import cc.vv.btpartyjob.partymanager.moudel.resp.PromisePingJiaResponseObj
import cc.vv.btpartyjob.partymanager.server.PartyManagerServer
import cc.vv.mvp.activity.BaseActivityMVP
import cc.vv.mvp.binder.BaseDataBinder
import com.chad.library.adapter.base.BaseQuickAdapter

/**
 * 承诺详情评价列表
 * Created by Chen.qi on 2018/10/29.
 */
class PartyPromisePingjiaListActivity : BaseActivityMVP<PartyPromisePingjiaListActivityDelegate>()
        , SwipeRefreshLayout.OnRefreshListener,
        BaseQuickAdapter.RequestLoadMoreListener {
    /**
     * 当前页数
     */
    private var pageNo: Int = 1
    /**
     * 每页条数
     */
    private val pageSize: Int = 10

    /**
     * 网格适配器
     */
    private var mAdapter: PartyPromisePingJiaListAdapter? = null

    /**
     * 集合数据
     */
    private var mDataList: ArrayList<PromisePingJiaObj>? = null

    private var id: String? = null

    private var headView: View? = null

    /**
     * 0--党员
     * 1---单位
     */
    private var type: Int? = 0

    private var title: String? = null


    override fun initData() {
        try {
            id = intent.getStringExtra("id")
            title = intent.getStringExtra("title")
            type = intent.getIntExtra("type", 0)
            viewDelegate?.setTopBarTitle(title, false)
        } catch (e: Exception) {
        }
        mDataList = ArrayList()
        if (mAdapter == null) {
            headView = LayoutInflater.from(this)
                    .inflate(R.layout.activity_promise_detail, null)
            mAdapter = PartyPromisePingJiaListAdapter(R.layout.item_promise_pingjia_list)
            mAdapter!!.addHeaderView(headView)
            viewDelegate?.setRecyclerViewAdapter(mAdapter)
            mAdapter?.setLoadMoreView(BTLoadMoreView())
            viewDelegate?.setAdapterData(mAdapter, mDataList)
        }

        PartyManagerServer.partyPromiseDetailUrl(id)
        PartyManagerServer.partyPromisePingJiaList(id, pageNo, pageSize, true)
    }

    override fun bindEvenListener() {
        viewDelegate?.setTitleBackBtnOnClick(object : BaseTopBarView.EventInterface {
            //返回
            override fun leftOnClick() {
                finish()
            }

            override fun leftCloseClick() {}
            override fun rightOnClick() {
                val intent = Intent(this@PartyPromisePingjiaListActivity, SubmitPingJiaActivity::class.java)
                intent.putExtra("id", id)
                startActivityForResult(intent, 100)
            }
        })
        viewDelegate?.bindRefreshListener(this)
        viewDelegate?.bindLoadMore(mAdapter, this)

    }

    override fun getDelegateClass(): Class<PartyPromisePingjiaListActivityDelegate> {
        return PartyPromisePingjiaListActivityDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return ManagerBaseBinder()
    }

    override fun getData(obj: BaseResponseObj<*>?) {
        super.getData(obj)
        when (obj) {
            is PromiseDetailResponseObj -> {
                val viewName = headView?.findViewById<TextView>(R.id.tv_apdD_userName)
                val viewType = headView?.findViewById<TextView>(R.id.tv_apdD_type)
                val viewContent = headView?.findViewById<TextView>(R.id.tv_apdD_content)
                val viewTime = headView?.findViewById<TextView>(R.id.tv_apdD_time)
                val viewPhone = headView?.findViewById<TextView>(R.id.tv_apdD_phone)
                val data = obj.data
                if (type == 0) {
                    viewType?.text = "驻区党员承诺"
                    viewName?.text = data?.userName
                } else if (type == 1) {
                    viewType?.text = "驻区单位承诺"
                    viewName?.text = data?.committedUnitName
                }

                viewContent?.text = data?.content
                viewTime?.text = OperationServer.formatStr(data?.completeTime)
                viewPhone?.text = data?.mobile

                if (data?.createUser == LKPrefUtil.getString(UserInfoSharePreKey.USER_ID, "")) {
                    viewDelegate?.setTopBarTitle(title, false)
                } else {
                    viewDelegate?.setTopBarTitle(title, true)
                }

            }

            is PromisePingJiaResponseObj -> {
                viewDelegate?.stopRefreshing()
                if (null == obj.data) {
                    return
                }
                if (1 == pageNo) {
                    mDataList?.clear()
                    if (null != obj.data.records) {
                        mDataList!!.addAll(obj.data.records)
                    }
                    viewDelegate?.setAdapterData(mAdapter, mDataList)
                } else {
                    if (mAdapter?.data?.size!! >= obj.data.total) {
                        //数据全部加载完毕
                        mAdapter?.loadMoreEnd(true)
                        mAdapter?.loadMoreEnd()
                    } else {
                        //成功获取更多数据
                        if (null != obj.data.records) {
                            mDataList!!.addAll(obj.data.records)
                        }
                        mAdapter?.setNewData(mDataList)
                        //加载数据
                        mAdapter?.disableLoadMoreIfNotFullPage()
                        mAdapter?.loadMoreComplete()
                    }
                }
            }
        }

    }

    override fun getBadCode(obj: BaseResponseObj<*>?) {
        super.getBadCode(obj)
        if (1 == pageNo) {
            viewDelegate?.stopRefreshing()
        }
        mAdapter?.loadMoreComplete()
        mAdapter?.loadMoreFail()
    }

    override fun onHttpFailure(url: String?, exceptionStr: String?) {
        super.onHttpFailure(url, exceptionStr)
        if (1 == pageNo) {
            viewDelegate?.stopRefreshing()
        }
    }


    override fun onRefresh() {
        mAdapter?.setEnableLoadMore(false)//这里的作用是防止下拉刷新的时候还可以上拉加载
        pageNo = 1
        PartyManagerServer.partyPromisePingJiaList(id, pageNo, pageSize, false)

    }

    override fun onLoadMoreRequested() {
        if (mDataList!!.size < 10) {
            mAdapter?.loadMoreComplete()
            mAdapter?.loadMoreEnd(true)
            return
        }
        pageNo++
        PartyManagerServer.partyPromisePingJiaList(id, pageNo, pageSize, false)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 100 && resultCode == 101) {
            sendBroadcast(Intent(BroadCastJniKey.BROAD_CAST_KEY_PINGJIA))
            pageNo = 1
            PartyManagerServer.partyPromisePingJiaList(id, pageNo, pageSize, true)
        }
    }
}